package com.daiziru.learning.juc;

/**
 * 作者：無言
 * 时间：2024/12/5 19:08
 * 描述：永无BUG
 */
public class Wait {


    public static void main(String[] args) throws Exception {

        Object lock = new Object();


        Runnable runnable = () -> {

            try {
                System.out.println("线程1");
                synchronized (lock) {
                    System.out.println("线程1开始等待");
                    //如果设置了等待时间，时间结束会自动唤醒，不需要notify唤醒
                    lock.wait(3000);
                }
                System.out.println("线程1结束等待");

            } catch (Exception e) {
                e.printStackTrace();
            }

        };
        Thread thread1 = new Thread(runnable, "线程1");


        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    System.out.println("唤醒线程1");
                    lock.notifyAll();
                }
            }
        }, "线程2");

        thread1.start();

        Thread.sleep(3000);

        thread2.start();


    }
}
